package cn.edu.gdut.deepcode.controller;


import cn.edu.gdut.deepcode.domain.dto.CanvasDTO;
import cn.edu.gdut.deepcode.domain.po.Result;
import cn.edu.gdut.deepcode.domain.vo.CanvasVO;
import cn.edu.gdut.deepcode.service.ICanvasService;
import cn.edu.gdut.deepcode.utils.CommonUtils;
import cn.edu.gdut.deepcode.context.UserContext;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.edu.gdut.deepcode.constant.AbnormalInfoConstant.CI_EI_2;
import static cn.edu.gdut.deepcode.constant.ReqRespConstant.AUTHORIZATION_HEADER_NAME;

/**
 * <p>
 * 画布基础信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-11
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/canvas")
public class CanvasController {

    private final ICanvasService canvasService;

    @ApiOperation(value = "获取所有画布基础信息接口")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @GetMapping
    public Result<List<CanvasVO>> getAllCanvasInfo() throws JsonProcessingException {
        return canvasService.getAllCanvasInfo();
    }

    @ApiOperation(value = "新增画布基础信息接口", notes = "响应体中的data属性为画布id")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @PostMapping
    public Result<Long> saveCanvasInfo(@RequestBody CanvasDTO canvasDTO) {
        if (BeanUtil.isEmpty(canvasDTO)) {
            // 画布基础信息异常
            CommonUtils.recordErrorInfoAndThrowException(
                    CI_EI_2,
                    canvasDTO
            );
        }
        return canvasService.saveCanvasInfo(canvasDTO);
    }


    @ApiOperation(value = "修改画布基础信息接口", notes = "响应体中的data属性为null")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @PutMapping("/title")
    public Result<?> updateCanvasTitle(@RequestBody CanvasDTO canvasDTO) {
        if (canvasDTO == null ||
                canvasDTO.getTitle() == null ||
                canvasDTO.getCanvasId() == null
        ) {
            // 画布基础信息异常
            CommonUtils.recordErrorInfoAndThrowException(
                    CI_EI_2,
                    List.of(UserContext.getUser(), canvasDTO)
            );
        }
        return canvasService.updateCanvasTitle(canvasDTO);
    }

    @PutMapping("/img")
    public Result<?> updateCanvasImg(@RequestBody CanvasDTO canvasDTO) {
        if (canvasDTO == null ||
                StrUtil.isBlank(canvasDTO.getImg()) ||
                canvasDTO.getCanvasId() == null ||
                canvasDTO.getZoom() == null
        ) {
            // 画布基础信息异常
            CommonUtils.recordErrorInfoAndThrowException(
                    CI_EI_2,
                    List.of(UserContext.getUser(), canvasDTO)
            );
        }
        return canvasService.updateCanvasImg(canvasDTO);

    }

    @ApiOperation(value = "删除画布基础信息接口", notes = "响应体中的data属性为null")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @DeleteMapping("/{canvasId}")
    public Result<?> deleteCanvasInfoById(@PathVariable("canvasId") Long canvasId) {
        return canvasService.deleteCanvasInfoById(canvasId);
    }

    /**
     * <p>
     * 根据画布id查询画布基础信息
     * </p>
     *
     * @Author: myc
     * @Since: 2025-04-13  18:01
     * @Version: 1.0.0
     */
    @ApiOperation(value = "根据画布id查询画布基础信息接口")
    @ApiImplicitParam(
            name = AUTHORIZATION_HEADER_NAME,
            value = "JWT 令牌",
            required = true,
            dataType = "string",
            paramType = "header"
    )
    @GetMapping("/{canvasId}")
    public Result<CanvasVO> getCanvasInfoById(@PathVariable("canvasId") Long canvasId) throws JsonProcessingException {
        return canvasService.getCanvasInfoById(canvasId);
    }
}
